﻿Imports System
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Runtime.Tracking
    Public NotInheritable Class ActivityTrackPoint
        ' Methods
        Friend Function IsMatch(ByVal activity As Activity, ByVal status As ActivityExecutionStatus) As Boolean
            Dim flag As Boolean = False
            Dim location As ActivityTrackingLocation
            For Each location In Me._match
                If (location.Match(activity, False) AndAlso location.ExecutionStatusEvents.Contains(status)) Then
                    flag = True
                    Exit For
                End If
            Next
            If Not flag Then
                Return False
            End If
            Dim location2 As ActivityTrackingLocation
            For Each location2 In Me._exclude
                If (location2.Match(activity, False) AndAlso location2.ExecutionStatusEvents.Contains(status)) Then
                    Return False
                End If
            Next
            Return flag
        End Function

        Friend Function IsMatch(ByVal activity As Activity, <Out> ByRef status As List([Of] ActivityExecutionStatus), <Out> ByRef hasCondition As Boolean) As Boolean
            hasCondition = False
            Dim location As ActivityTrackingLocation
            For Each location In Me._exclude
                If ((Not location.Conditions Is Nothing) AndAlso (location.Conditions.Count > 0)) Then
                    hasCondition = True
                    Exit For
                End If
            Next
            Dim location2 As ActivityTrackingLocation
            For Each location2 In Me._match
                If ((Not location2.Conditions Is Nothing) AndAlso (location2.Conditions.Count > 0)) Then
                    hasCondition = True
                    Exit For
                End If
            Next
            status = New List([Of] ActivityExecutionStatus)(9)
            Dim location3 As ActivityTrackingLocation
            For Each location3 In Me._match
                If location3.Match(activity, True) Then
                    Dim status2 As ActivityExecutionStatus
                    For Each status2 In location3.ExecutionStatusEvents
                        If Not status.Contains(status2) Then
                            status.Add(status2)
                        End If
                    Next
                    [Continue] For
                End If
            Next
            If (status.Count = 0) Then
                Return False
            End If
            If Not hasCondition Then
                Dim location4 As ActivityTrackingLocation
                For Each location4 In Me._exclude
                    If location4.Match(activity, True) Then
                        Dim status3 As ActivityExecutionStatus
                        For Each status3 In location4.ExecutionStatusEvents
                            status.Remove(status3)
                        Next
                        [Continue] For
                    End If
                Next
            End If
            Return (status.Count > 0)
        End Function

        Friend Sub Track(ByVal activity As Activity, ByVal provider As IServiceProvider, ByVal items As IList([Of] TrackingDataItem))
            Dim extract As TrackingExtract
            For Each extract In Me._extracts
                extract.GetData(activity, provider, items)
            Next
        End Sub


        ' Properties
        Public ReadOnly Property Annotations As TrackingAnnotationCollection
            Get
                Return Me._annotations
            End Get
        End Property

        Public ReadOnly Property ExcludedLocations As ActivityTrackingLocationCollection
            Get
                Return Me._exclude
            End Get
        End Property

        Public ReadOnly Property Extracts As ExtractCollection
            Get
                Return Me._extracts
            End Get
        End Property

        Public ReadOnly Property MatchingLocations As ActivityTrackingLocationCollection
            Get
                Return Me._match
            End Get
        End Property


        ' Fields
        Private _annotations As TrackingAnnotationCollection = New TrackingAnnotationCollection
        Private _exclude As ActivityTrackingLocationCollection = New ActivityTrackingLocationCollection
        Private _extracts As ExtractCollection = New ExtractCollection
        Private _match As ActivityTrackingLocationCollection = New ActivityTrackingLocationCollection
    End Class
End Namespace

